/*     */ package com.zimbra.cs.service.mail;
/*     */ 
/*     */ import com.zimbra.common.account.Key.AccountBy;
/*     */ import com.zimbra.common.account.Key.CalendarResourceBy;
/*     */ import com.zimbra.common.account.Key.DistributionListBy;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.soap.Element;
/*     */ import com.zimbra.common.soap.MailConstants;
/*     */ import com.zimbra.cs.account.AccessManager;
/*     */ import com.zimbra.cs.account.AccountServiceException;
/*     */ import com.zimbra.cs.account.NamedEntry;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.accesscontrol.RightManager;
/*     */ import com.zimbra.cs.account.accesscontrol.TargetType;
/*     */ import com.zimbra.cs.account.accesscontrol.UserRight;
/*     */ import com.zimbra.cs.service.account.DiscoverRights;
/*     */ import com.zimbra.cs.util.AccountUtil;
/*     */ import com.zimbra.soap.ZimbraSoapContext;
/*     */ import com.zimbra.soap.type.TargetBy;
/*     */ import java.util.ArrayList;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class CheckPermission
/*     */   extends MailDocumentHandler
/*     */ {
/*     */   public Element handle(Element request, Map<String, Object> context)
/*     */     throws ServiceException
/*     */   {
/*  44 */     ZimbraSoapContext zsc = getZimbraSoapContext(context);
/*  45 */     Provisioning prov = Provisioning.getInstance();
/*     */     
/*  47 */     Element eTarget = request.getElement("target");
/*  48 */     String targetType = eTarget.getAttribute("type");
/*  49 */     TargetType tt = TargetType.fromCode(targetType);
/*     */     
/*  51 */     String targetBy = eTarget.getAttribute("by");
/*     */     
/*  53 */     String targetValue = eTarget.getText();
/*     */     
/*  55 */     NamedEntry entry = null;
/*     */     
/*  57 */     Element response = zsc.createElement(MailConstants.CHECK_PERMISSION_RESPONSE);
/*     */     
/*  59 */     if (TargetType.account == tt) {
/*  60 */       Key.AccountBy acctBy = Key.AccountBy.fromString(targetBy);
/*  61 */       entry = prov.get(acctBy, targetValue, zsc.getAuthToken());
/*     */       
/*  63 */       if ((entry == null) && (acctBy == Key.AccountBy.id)) {
/*  64 */         throw AccountServiceException.NO_SUCH_ACCOUNT(targetValue);
/*     */ 
/*     */       }
/*     */       
/*     */ 
/*     */     }
/*  70 */     else if (TargetType.calresource == tt) {
/*  71 */       Key.CalendarResourceBy crBy = Key.CalendarResourceBy.fromString(targetBy);
/*  72 */       entry = prov.get(crBy, targetValue);
/*     */       
/*  74 */       if ((entry == null) && (crBy == Key.CalendarResourceBy.id)) {
/*  75 */         throw AccountServiceException.NO_SUCH_CALENDAR_RESOURCE(targetValue);
/*     */       }
/*     */     }
/*  78 */     else if (TargetType.dl == tt) {
/*  79 */       Key.DistributionListBy dlBy = Key.DistributionListBy.fromString(targetBy);
/*  80 */       entry = prov.getGroupBasic(dlBy, targetValue);
/*     */       
/*  82 */       if ((entry == null) && (dlBy == Key.DistributionListBy.id)) {
/*  83 */         throw AccountServiceException.NO_SUCH_CALENDAR_RESOURCE(targetValue);
/*     */       }
/*     */     }
/*     */     else {
/*  87 */       throw ServiceException.INVALID_REQUEST("invalid target type: " + targetType, null);
/*     */     }
/*     */     
/*  90 */     List<UserRight> rights = new ArrayList();
/*  91 */     for (Element eRight : request.listElements("right")) {
/*  92 */       UserRight r = RightManager.getInstance().getUserRight(eRight.getText());
/*  93 */       rights.add(r);
/*     */     }
/*     */     
/*  96 */     boolean finalResult = true;
/*  97 */     AccessManager am = AccessManager.getInstance();
/*  98 */     for (UserRight right : rights) {
/*  99 */       boolean allow = am.canDo(zsc.getAuthToken(), entry, right, false);
/* 100 */       if ((allow) && (DiscoverRights.isDelegatedSendRight(right)) && (TargetBy.name.name().equals(targetBy)))
/*     */       {
/*     */ 
/* 103 */         allow = AccountUtil.isAllowedSendAddress(entry, targetValue);
/*     */       }
/* 105 */       response.addElement("right").addAttribute("allow", allow).setText(right.getName());
/* 106 */       finalResult &= allow;
/*     */     }
/*     */     
/* 109 */     return returnResponse(response, finalResult);
/*     */   }
/*     */   
/*     */   private Element returnResponse(Element response, boolean allow) {
/* 113 */     response.addAttribute("allow", allow);
/* 114 */     return response;
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/mail/CheckPermission.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */